<?php
/**
 * Service of Customer
 *
 * @author Steve
 */

require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("dbgrid")  .  '/DBGrid.php';
require_once BaseUtil::getModulePath("dbgrid")  .  '/DBGridConstant.php';
require_once BaseUtil::getModulePath("gift")  .  '/models/ApplyGiftLogDao.php';
require_once BaseUtil::getModulePath("gift")  .  '/models/GiftDao.php';

class GiftService extends BaseService {
	private $applyGiftLogDao;
	private $giftDao;
	
	public function __construct(){
		parent::__construct();
		$this->applyGiftLogDao = new ApplyGiftLogDao();
		$this->applyGiftLogDao->setDb($this->db);
		
		$this->giftDao = new GiftDao();
		$this->giftDao->setDb($this->db);
	}
	
	
	public function getApplyGiftLogList($post,$view){
		$applyTypeArr = array('1'=>$this->_translate->translate("applyCoupon"), '2'=>$this->_translate->translate('applyGift'));
		
		$statusArr = GiftConstant::getApplyGiftStatusList();
		foreach(GiftConstant::getApplyCouponStatusList() as $key=>$value){
			$statusArr[$key] = $value;
		}
		
		$dbGrid = new DBGrid($view);
		$dbGrid->setPost($post);
		$dbGrid->setModule('/gift/applygiftlog');
		
		$dbGrid->addFetchTable('apply_gift_logs as agl','agl.*');
		$dbGrid->addFetchTable('users as u','u.name','agl.user_id=u.id');
		$dbGrid->addValueFilter('agl-is_delete',0);
		$dbGrid->addValueFilter('u.is_delete',0);
		
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,'agl-id',$this->_translate->translate("id"),DBGridConstant::OPERATOR_EQUAL);
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"u-name", $this->_translate->translate("userName"));
		$dbGrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "agl-apply_type", $this->_translate->translate ( "applyType" ), $applyTypeArr);
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"agl-coupon_code", $this->_translate->translate("couponCode"));
		$dbGrid->addBetweenFilter("agl-points",$this->_translate->translate("applyUsedPoint"));
		
		$dbGrid->addFetchColumn("id",$this->_translate->translate("id"),"edit");
		$dbGrid->addFetchColumn("name",$this->_translate->translate("userName"));
		$dbGrid->addFetchColumn("apply_type",$this->_translate->translate("applyType"),"",$applyTypeArr);
		$dbGrid->addFetchColumn("coupon_code",$this->_translate->translate("couponCode"));
		$dbGrid->addFetchColumn("points",$this->_translate->translate("applyUsedPoint"));
		$dbGrid->addFetchColumn("status",$this->_translate->translate("status"),"",$statusArr);

		$dbGrid->addFetchColumn("created",$this->_translate->translate("applyDate"));
		$dbGrid->setSortOrder("agl-created");
        $dbGrid->setPageTitle($this->_translate->translate("applyGiftLog"));
        return $dbGrid->render();
		
	}
	
	public function getApplyGiftLogById($id){
		return $this->applyGiftLogDao->getApplyGiftLogById($id);
	}
	
	public function getGiftById($id){
		return $this->giftDao->getGiftById($id);
	}
	
	public function deleteApplyGiftLog($id){
		$this->applyGiftLogDao->deleteApplyGiftLog($id);
	}
	
	public function updateApplyGiftLog($data,$id){
		$this->applyGiftLogDao->updateApplyGiftLog($data,$id);
	}
	
	public function addGift($gift){
		return $this->giftDao->insertData($gift);
	}
	
	public function updateGift($gift,$id){
		$this->giftDao->updateGift($gift,$id);
	}
	
	public function deleteGift($id){
		$this->giftDao->deleteGift($id);
	}
	
	public function getGiftList($post,$view){
		$dbGrid = new DBGrid($view);
		$dbGrid->setPost($post);
		$dbGrid->setModule('/gift/gift');
		
		$dbGrid->addFetchTable('gifts as g','g.*');
		$dbGrid->addFetchTable('product_items as pi','pi.product_id','g.product_item_id=pi.id',DBGridConstant::JOIN_TYPE_OUTER);
		$dbGrid->addValueFilter('g-is_delete',0);
		
		$dbGrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT,"g-title", $this->_translate->translate("title"));
		
		$dbGrid->addFetchColumn("id",$this->_translate->translate("id"),"edit");
		$dbGrid->addFetchColumn("title",$this->_translate->translate("title"));
		$dbGrid->addFetchColumn("exhaust_points",$this->_translate->translate("exhaustPoint"));
		$dbGrid->addFetchColumn("min_ac_points",$this->_translate->translate("minACPoint"));
		$dbGrid->addFetchColumn("start_date",$this->_translate->translate("startDate"));
        $dbGrid->addFetchColumn("end_date",$this->_translate->translate("endDate"));
	    if(AAService::isAllowed("gift", "add")){
            $dbGrid->addModuleAction("add",$this->_translate->translate("addBtn"),"add");
        }
        
       // $dbGrid->setSortOrder("g-created");
        $dbGrid->setPageTitle($this->_translate->translate("giftList"));
        return $dbGrid->render();
		
		
	}
}
?>